package com.ruoyi.web.controller.api.recipe;

import com.ruoyi.api.domain.recipe.CategoryDishEntity;
import com.ruoyi.api.domain.recipe.CategoryEntity;
import com.ruoyi.api.domain.recipe.DishEntity;
import com.ruoyi.common.annotation.NoLoginRequired;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.recipe.domain.RecipeCategory;
import com.ruoyi.recipe.domain.RecipeDish;
import com.ruoyi.recipe.domain.RecipeSlidePic;
import com.ruoyi.recipe.service.IRecipeCategoryService;
import com.ruoyi.recipe.service.IRecipeDishService;
import com.ruoyi.recipe.service.IRecipeSlidePicService;
import com.ruoyi.web.controller.api.APIController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Api("食谱类别")
@RestController
@RequestMapping("/api/category")
@NoLoginRequired
public class CategoryController extends APIController {
    @Autowired
    private IRecipeCategoryService recipeCategoryService;

    @Autowired
    private IRecipeDishService recipeDishService;

    @Autowired
    private IRecipeSlidePicService recipeSlidePicService;

    @ApiOperation("获取首页食谱类别")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openId", value = "微信openId", dataType = "String", dataTypeClass = String.class)
    })
    @PostMapping("/list")
    public AjaxResult list(@RequestParam(value = "openId", required = false, defaultValue = "")String openId)
    {
        String baseUrl = getBaseUrl();
        RecipeCategory recipeCategory = new RecipeCategory();
        recipeCategory.setStatus("0");
        recipeCategory.setVisible("0");
        List<RecipeCategory> categoryList = recipeCategoryService.selectCategoryList(recipeCategory);
        RecipeDish recipeDish = new RecipeDish();
        recipeDish.setLimit(3);
        recipeDish.setOpenId(openId);
        recipeDish.setStatus("0");
        List<RecipeDish> dishRecommendedList = recipeDishService.selectRandRecipeDish(recipeDish);
        // 使用 Stream 取前 3 条记录
        List<RecipeDish> dishList = dishRecommendedList.stream().limit(3).collect(Collectors.toList());

        List<RecipeSlidePic> slidePicList = recipeSlidePicService.selectRecipeSlidePicList(null);
        List<CategoryEntity> categoryEntityList = new ArrayList<CategoryEntity>();
        List<DishEntity> dishEntityList = new ArrayList<DishEntity>();
        CategoryDishEntity entity = new CategoryDishEntity();
        for (RecipeCategory category : categoryList) {
            CategoryEntity tmp = new CategoryEntity();
            BeanUtils.copyBeanProp(tmp, category);
            if(StringUtils.isNotEmpty(tmp.getCategoryPic()) && !StringUtils.ishttp(tmp.getCategoryPic())) {
                String fullDishPic = baseUrl + tmp.getCategoryPic();
                tmp.setCategoryPic(fullDishPic);
            }
            categoryEntityList.add(tmp);
        }
        // dishList按照likesCount降序排序
        dishList.sort((o1, o2) -> o2.getLikesCount() - o1.getLikesCount());
        for (RecipeDish dish : dishList) {
            DishEntity tmp = new DishEntity();
            BeanUtils.copyBeanProp(tmp, dish);
            if(StringUtils.isNotEmpty(tmp.getDishPic()) && !StringUtils.ishttp(tmp.getDishPic())) {
                String fullDishPic = baseUrl + tmp.getDishPic();
                tmp.setDishPic(fullDishPic);
            }
            dishEntityList.add(tmp);
        }
        for (RecipeSlidePic pic : slidePicList) {
            if(StringUtils.isNotEmpty(pic.getPicUrl()) && !StringUtils.ishttp(pic.getPicUrl())) {
                String fullDishPic = baseUrl + pic.getPicUrl();
                pic.setPicUrl(fullDishPic);
            }
        }

        entity.setCategory(categoryEntityList);
        entity.setDish(dishEntityList);
        entity.setPic(slidePicList);
        return AjaxResult.success(entity);
    }
}
